#!/usr/bin/env python
# -*- coding:UTF-8 -*-
__author__ = 'wycao'

from selenium import webdriver
from pgre.postgkeyword import PostKeyword
import  time,logging,sys
from jiebatitle.jbtitle import JbTitle


class SeAnnouncement:
    def __init__(self):
        self.dr = webdriver.Chrome()
        self.dr.implicitly_wait(15)
        self.dr.delete_all_cookies()
        url = r'http://www.cninfo.com.cn/cninfo-new/disclosure/szse'
        self.dr.get(url=url)
        self.pg = PostKeyword()
        self.jb = JbTitle()

    def get_report_type(self):
        #点击选择公告类别搜索框
        self.dr.find_element_by_xpath("//form[@id='AnnoucementsQueryForm']//div[@class='search-condition c5 drop-down']/a/div").click()
        type_li = self.dr.find_elements_by_xpath("//form[@id='AnnoucementsQueryForm']//div[@class='search-condition c5 drop-down']/ul/li")
        #点击第1 个类别(年度报告)
        time.sleep(1)
        logging.info(sys.stdout)
        self.dr.find_element_by_xpath("//ul[@id='category_list']/li[1]/a").click()
        handls = self.dr.window_handles
        #切换到年度报告页面
        self.dr.switch_to.window(handls[1])

    def get_show_list(self,type):
        # list_li = self.dr.find_elements_by_xpath("//ul[@id='ul_his_fulltext']/li")
        row = 0
        try:
            for r in range(80000):
                values = []
                values.append(str(type))
                code = self.dr.find_element_by_xpath("//ul[@id='ul_his_fulltext']/li["+str(r+1)+"]/div[1]/font").text
                values.append(code.rstrip())
                shotname = self.dr.find_element_by_xpath("//ul[@id='ul_his_fulltext']/li["+str(r+1)+"]/div[2]/font").text
                values.append(shotname.rstrip())
                title = self.dr.find_element_by_xpath("//ul[@id='ul_his_fulltext']/li["+str(r+1)+"]/div[3]/dd/span[1]/a").text
                values.append(title.rstrip())
                title_href =self.dr.find_element_by_xpath("//ul[@id='ul_his_fulltext']/li["+str(r+1)+"]/div[3]/dd/span[1]/a").get_attribute("href")
                values.append(title_href.rstrip())
                show_date = self.dr.find_element_by_xpath("//ul[@id='ul_his_fulltext']/li["+str(r+1)+"]/div[3]/dd/span[2]").text
                values.append(show_date.rstrip())
                #拆分标题
                title_list = self.jb.cut_text(title.rstrip())
                if '半年报告' in title_list:
                    tindex = title_list.index('半年报告')
                    ptdate_list =title_list[:tindex]
                    portdate = "".join(ptdate_list)
                    port_type = '半年报告'
                elif '年度报告' in title_list:
                    tindex = title_list.index('年度报告')
                    ptdate_list =title_list[:tindex]
                    portdate = "".join(ptdate_list)
                    port_type = '年度报告'
                elif '第一季报告' in title_list:
                    tindex = title_list.index('第一季报告')
                    ptdate_list =title_list[:tindex]
                    portdate = "".join(ptdate_list)
                    port_type = '第一季报告'
                elif '第三报告' in title_list:
                    tindex = title_list.index('第三报告')
                    ptdate_list =title_list[:tindex]
                    portdate = "".join(ptdate_list)
                    port_type = '第三报告'
                # if '(' in title_list:
                #     stindex = title_list.index('(')
                #     ptstatus_list = title_list[stindex+1:]
                portstatus = title_list[-7]
                values.append(str(portdate))
                values.append(str(port_type))
                values.append(str(portstatus))
                self.pg.insert_data(values=values)
                print("{0}入库成功".format(shotname))
                logging.info("{0}入库成功".format(shotname))
                row +=1
                print(row)
                #如果是 30的倍数,则点击'更多'
                if row % 30 ==0:
                    self.dr.find_element_by_xpath("//div[@id='con-div-his-fulltext']/div[5]/a").click()
                    for t in range(3):
                        #点击更多后,如果数量没有变,再次点击'更多'
                        list_li = self.dr.find_elements_by_xpath("//ul[@id='ul_his_fulltext']/li")
                        if len(list_li) == row:
                            self.dr.find_element_by_xpath("//div[@id='con-div-his-fulltext']/div[5]/a").click()
                        break
        except BaseException as e:
            print(e)
        finally:
            self.pg.conn.close()
            print("数据库链接关闭成功")



